package com.chittoda.koncurrent;

import java.util.concurrent.BlockingQueue;

/**
 * Blocking worker that will keep on taking the Task from its queue.
 * Worker will block when there is no task in the queue.
 * @author jitendra
 *
 */
public class Worker implements Runnable {

	//Task Queue
	private final BlockingQueue<Runnable> taskQueue;
	
	public Worker(BlockingQueue<Runnable> taskQueue) {
		this.taskQueue = taskQueue;
	}
	
	@Override
	public void run() {
		
		while(true) {
			
			try {
				Runnable task = taskQueue.take();
				//Calling Runnable.run() as this Worker is already running
				//in a thread
				task.run();
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}

	
}
